<?php

/**
 *
 *            ┏┓　　  ┏┓+ +
 *           ┏┛┻━━━━━┛┻┓ + +
 *           ┃         ┃ 　
 *           ┃   ━     ┃ ++ + + +
 *          ████━████  ┃+
 *           ┃　　　　　 ┃ +
 *           ┃　　　┻　　┃
 *           ┃　　　　　 ┃ + +
 *           ┗━┓　　　┏━┛
 *             ┃　　　┃　　　　
 *             ┃　　　┃ + + + +
 *             ┃　　　┃    Code is far away from bug with the alpaca protecting
 *             ┃　　　┃ + 　　　　        神兽保佑,代码无bug
 *             ┃　　　┃
 *             ┃　　　┃　　+
 *             ┃     ┗━━━┓ + +
 *             ┃         ┣┓
 *             ┃ 　　　　　┏┛
 *             ┗┓┓┏━━┳┓┏━┛ + + + +
 *              ┃┫┫  ┃┫┫
 *              ┗┻┛  ┗┻┛+ + + +
 * Created by PhpStorm.
 * User: weaponhsu
 * Date: 2019/2/2
 * Time: 10:29 AM
 */

namespace Task;


use Swoole\Timer;
use swoole_client;
use Yaf\Exception;
use Yaf\Registry;

class client
{
    private $client;
    public $log_path;
    // 本地开发环境
//    private $host = 'http://www.ylmall.com';
    // 线上环境
    private $host = 'http://api.douziyoupin.com';

    // swoole_server_ip
    private $server = '127.0.0.1';
    // swoole_server_port
    private $port = '19888';
    private $timeout = 1;

    public function __construct() {
        $this->client = new swoole_client(SWOOLE_SOCK_TCP);
        $this->log_path = realpath(__DIR__) . '/log/swoole_client' . date('Y-m-d', time()) . '.log';
    }

    public function getHDKProductIntoDb($product_arr = []) {
        $param = [
            'url' => $this->host . '/common/common/getTbkProductIntoDb',
            'param' => $product_arr
        ];
        $this->client->send(json_encode($param));
    }

    /**
     * 定时采集好单库的商品
     */
    public function getTbkProduct() {
        $current_time = time();
        $param = [
            'timeout' => 'all',
            'url' => $this->host . '/common/common/getTbkProductByTiming',
            'param' => ['time' => date("Y-m-d H:i:s", $current_time), 'deadline' => date("Y-m-d H:i:s", $current_time + 300)]
        ];
        $this->client->send(json_encode($param));
    }

    /**
     * 接收商品编号 商品数量 after的时间，用以调用处理拼团结果
     * @param $product_id
     * @param $num
     * @param $timeout
     */
    public function couponAlgorithm($product_id, $num, $timeout) {
        $current_time = time();
        $param = [
            'timeout' => $timeout,
            'url' => $this->host . '/common/common/couponResult',
            'param' => ['product_id' => $product_id, 'num' => $num, 'time' => date("Y-m-d H:i:s", $current_time), 'deadline' => date("Y-m-d H:i:s", $current_time + 300)]
        ];
        $this->client->send(json_encode($param));
    }

    /**
     * 拼团结果的推送消息
     * @param $sn
     * @param int $timeout
     */
    public function sendCouponRes($sn, $timeout = 0) {
        $param = [
            'url' => $this->host . '/common/common/couponPush',
            'param' => [
                'sn' => $sn
            ]
        ];
        if ($timeout)
            $param['timeout'] = $timeout;

        $this->client->send(json_encode($param));
    }

    /**
     * 订单状态的推送消息
     * @param $sn
     * @param int $timeout
     */
    public function sendPush4Order($sn, $timeout = 0) {
        $param = [
            'url' => $this->host . '/common/common/orderPush',
            'param' => [
                'sn' => $sn
            ]
        ];
        if ($timeout)
            $param['timeout'] = $timeout;

        $this->client->send(json_encode($param));
    }

    /**
     * 发送短信验证码
     * @param $content
     * @param $mobile
     * @param int $timeout
     */
    public function sendMsg($content, $mobile, $timeout = 0) {
        $param = [
            'url' => $this->host . '/common/common/msg',
            'param' => ['mobile' => $mobile, 'content' => $content]
        ];
        if ($timeout)
            $param['timeout'] = $timeout;

        $this->client->send(json_encode($param));
    }

    /**
     * 获取指定商品分类下的所有分类 直到叶子级分类
     * @param $category_id
     * @param int $timeout
     */
    public function categoryGet($category_id, $timeout = 0) {
        $param = [
            'url' => $this->host . '/alibaba/alibaba/getCategory',
            'param' => ['category_id' => $category_id]
        ];
        if ($timeout)
            $param['timeout'] = $timeout;

        $this->client->send(json_encode($param));
    }

    /**
     * 获取指定商品的促销活动信息
     * @param $product_id
     * @param int $timeout
     */
    public function queryOfferDetailActivity($product_id, $timeout = 0) {
        $param = [
            'url' => $this->host . '/alibaba/alibaba/queryOfferDetailActivity',
            'param' => ['product_id' => $product_id]
        ];
        if ($timeout)
            $param['timeout'] = $timeout;

        $this->client->send(json_encode($param));
    }

    /**
     * 获取商品详情
     * @param $product_id
     * @param int $timeout
     */
    public function productCpsMediaProductInfo($product_id, $timeout = 0) {
        $param = [
            'url' => $this->host . '/alibaba/alibaba/productCpsMediaProductInfo',
            'param' => ['product_id' => $product_id]
        ];
        if ($timeout)
            $param['timeout'] = $timeout;

        $this->client->send(json_encode($param));
    }

    /**
     * 获取指定选品库下的商品列表
     * @param $group_id
     * @param $page
     * @param $page_size
     * @param string $feed_id
     * @param string $title
     * @param int $timeout
     */
    public function productListCybUserGroupFeed($group_id, $page, $page_size, $feed_id = '', $title = '', $timeout = 0) {
        $param = [
            'url' => $this->host . '/alibaba/alibaba/productListCybUserGroupFeed',
            'param' => [
                'group_id' => $group_id,
                'page' => $page,
                'page_size' => $page_size,
            ]
        ];
        if (!empty($feed_id))
            $param['param']['feed_id'] = $feed_id;
        if (!empty($title))
            $param['param']['title'] = $title;

        if ($timeout)
            $param['timeout'] = $timeout;

        $this->client->send(json_encode($param));

    }

    /**
     * 获取商品库列表
     * @param int $page
     * @param int $timeout
     */
    public function listCybUserGroup($page = 1, $timeout = 0) {
        $param = [
            'url' => $this->host . '/alibaba/alibaba/listCybUserGroup',
            'param' => ['page' => $page]
        ];
        if ($timeout)
            $param['timeout'] = $timeout;

        $this->client->send(json_encode($param));
    }

    /**
     * 调用1688退货退款接口
     * @param $refund_info
     * @param $order_info
     * @param int $timeout
     */
    public function alibabaRefundReturn($refund_info, $order_info, $timeout = 0) {
        $param = [
            'url' => $this->host . '/alibaba/alibaba/refundReturn',
            'param' => ['refund_info' => $refund_info, 'order_info' => $order_info]
        ];
        if ($timeout)
            $param['timeout'] = $timeout;

        $this->client->send(json_encode($param));

    }

    public function chkTbkOrderIsSettled($sn, $start_time, $end_time) {
        $param = [
            'url' => $this->host . '/common/crontab/getSettledTbkOrder',
            'param' => [
                'sn' => $sn,
                'start_time' => $start_time,
                'end_time' => $end_time,
            ]
        ];
        $this->client->send(json_encode($param));
    }

    /**
     * 关注1688商品
     * @param $product_id
     */
    public function alibabaProductFollowAction($product_id) {
        $param = [
            'url' => $this->host . '/alibaba/alibaba/follow',
            'param' => [
                'product_id' => $product_id
            ]
        ];
        $this->client->send(json_encode($param));
    }

    public function getTbkOwnerByOrderSn($sn) {
        $param = [
            'url' => $this->host . '/common/common/getOwnerByOrderSn',
            'param' => [
                'sn' => $sn
            ]
        ];
        $this->client->send(json_encode($param));
    }

    public function getDeliveryInfoBySn($order_sn) {
        $param = [
            'url' => $this->host . '/alibaba/alibaba/getDeliveryBySn',
            'param' => [
                'sn' => $order_sn
            ]
        ];
        $this->client->send(json_encode($param));
    }

    public function delay2PayOrRefund($trade_order_sn, $platform, $action = 'pay', $timeout = 60) {
        $param = [
            'url' => $this->host . '/common/common/delay2PayOrRefund',
            'param' => ['trade_order_sn' => $trade_order_sn, 'action' => $action, 'platform' => $platform]
        ];
        if ($timeout)
            $param['timeout'] = $timeout;

        $this->client->send(json_encode($param));
    }


    public function updateJqBooking($product_id, $timeout = 60) {
        $param = [
            'url' => $this->host . '/common/common/updateJqBooking',
            'param' => ['product_id' => $product_id]
        ];
        if ($timeout)
            $param['timeout'] = $timeout;

        self::log($this->log_path, __FUNCTION__ . ' data: ' . json_encode($param));
        $this->client->send(json_encode($param));
    }

    /**
     * 链接swoole服务端
     * @return $this
     * @throws \Exception
     */
    public function connect() {
        if( !$this->client->connect($this->server, $this->port , $this->timeout) ) {
            throw new \Exception('无法链接服务器', '001');
        }

        self::log($this->log_path, '链接成功');
        return $this;
    }

    /**
     * 断开与swoole服务端的链接
     * @return bool
     */
    public function close() {
        if ($this->client->isConnected() === true)
            $this->client->close();

        return true;
    }

    static protected function log($log_path = null, $content = '', $type = 'INFO') {
        if (!is_null($log_path)) {
            error_log('[' . date('Y-m-d H:i:s', time()) . '] - ' . $type . ' - '.$content . "\r\n", 3, $log_path);
        }
    }
}
